using System;
using System.Collections.Generic;
using System.Text;
using RideMe.Core.Parser;
using RideMe.Core.SortableCollection;

namespace ICSharpCode.TextEditor.Document
{
    public class RubyFoldingStrategy : IFoldingStrategy 
    {
        List<FoldMarker> _foldMarkers;

        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            if (_foldMarkers == null)
            {
                _foldMarkers = new List<FoldMarker>();
            }
            else
            {
                _foldMarkers.Clear();
            }

            SortableCollection<TopLevelEntity> topLevelEntities = parseInformation as SortableCollection<TopLevelEntity>;

            if (topLevelEntities == null) return null;

            foreach (TopLevelEntity entity in topLevelEntities)
            {
                if (entity.End != null)
                {
                     _foldMarkers.Add(new FoldMarker(document, entity.Line, entity.Column, entity.End.Line, entity.LineLength + entity.End.LineLength));
                }

                foreach (Method method in entity.Methods)
                {
                    if (method.End != null)
                    {
                        _foldMarkers.Add(new FoldMarker(document, method.Line, entity.Column, method.End.Line, method.LineLength + method.End.LineLength, FoldType.MemberBody));
                    }
                }

                foreach (TopLevelEntity subEntity in entity.SubEntities)
                {
                    foreach (Method method in subEntity.Methods)
                    {
                        if (method.End != null)
                        {
                            _foldMarkers.Add(new FoldMarker(document, method.Line, method.Column, method.End.Line, method.LineLength + method.End.LineLength, FoldType.MemberBody));
                        }
                    }
                    if (subEntity.End != null)
                    {
                        _foldMarkers.Add(new FoldMarker(document, subEntity.Line, entity.Column, subEntity.End.Line, subEntity.LineLength + subEntity.End.LineLength));
                    }
                }
            }

            return _foldMarkers;
        }

    }
}
